﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using SapaFinance.Domain.Model;

namespace SapaFinance.Domain.Extensions
{
    public static class Extensions
    {
        public static Money Sum(this IEnumerable<Order> source,
                                          Func<Order, Money> selector)
        {
            var orig = source.First().Payment;
          //  var t = orig.GetType();
            Object[] args = { source.First().Payment.Amount, orig.Currency };

            var dirty = (Money)Activator.CreateInstance(typeof(Money), args);
            foreach (var num2 in source)
                dirty += num2.Payment;
            return dirty - orig;
        }

        public static Money Sum(this IEnumerable<Request> source,
                                          Func<Request, Money> selector)
        {
            var orig = source.First().Payment;
         //   var t = orig.GetType();
            Object[] args = { source.First().Payment.Amount, orig.Currency };

            var dirty = (Money)Activator.CreateInstance(typeof(Money), args);
            foreach (var num2 in source)
                dirty += num2.Payment;
            return dirty - orig;
        }


        public static Money Sum(this IEnumerable<Share> source,
                                        Func<Share, Money> selector)
        {
                var orig = source.First().Amount;
                //   var t = orig.GetType();
                Object[] args = {source.First().Amount.Amount, orig.Currency};

                var dirty = (Money) Activator.CreateInstance(typeof (Money), args);
                foreach (var num2 in source)
                    dirty += num2.Amount;
                return dirty - orig;
        }

        public static decimal RoundTo(this decimal value, DecimalSize option)
        {
            return Decimal.Round(value, (int)option, MidpointRounding.AwayFromZero);
        }

        public static string GetDisplayName(this Type dataType)
        {
            // First look into attributes on a type and it's parents
            var attr = (DisplayNameAttribute)dataType.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
            // Look for [MetadataType] attribute in type hierarchy
            // http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class
            //if (attr == null)
            //{
            //    //MetadataTypeAttribute metadataType = (MetadataTypeAttribute)dataType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();
            //    ////if (metadataType != null)
            //    ////{
            //    ////    var property = metadataType.MetadataClassType.GetProperty(fieldName);
            //    ////    if (property != null)
            //    ////    {
            //    ////        attr = (DisplayNameAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
            //    ////    }
            //    ////}
            //}
            return (attr != null) ? attr.DisplayName : String.Empty;
        }
    }
}